//分析⼀天内用户每⼩时的行为

package homework.taobao.Work01.a.Reducer;

import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class TimeStatReducer extends Reducer<Text, Text, Text, NullWritable> {
    private final Text outKey = new Text();
    private final NullWritable outValue = NullWritable.get();
    @Override
    protected void reduce(Text key, Iterable<Text> values, Reducer<Text, Text, Text, NullWritable>.Context context) throws IOException, InterruptedException {

        String timeStr = key.toString();
        Map<String, Long> map = new HashMap<>();
        for (Text value : values) {
            String val = value.toString();
            map.merge(val, 1L, Long::sum);
        }
        for (Map.Entry<String, Long> entry : map.entrySet()) {
            String result = timeStr + "," + entry.getKey() + "," + entry.getValue();
            outKey.set(result);
            context.write(outKey, outValue);
        }
    }
}

